package demo16_1;

import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

public class Test16_1 {

    //输入两个整数序列，第一个序列表示栈的压入顺序，请判断第二个序列是否可能为该栈的弹出顺序。
    public boolean IsPopOrder (int[] pushV, int[] popV) {
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for(int i = 0;i < pushV.length; i++) {
            stack.push(pushV[i]);
            while(!stack.empty() && j < popV.length && stack.peek() == popV[j]) {
                stack.pop();
                j++;
            }
        }
        if(stack.empty()) {
            return true;
        }
        return false;
    }
    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for(int i = 0;i<s.length();i++) {
            char ch = s.charAt(i);
            if(ch == '(' || ch == '{' || ch == '[') {
                //左括号
                stack.push(ch);
            }else {
                //右括号
                if (stack.empty()) {
                    return false;
                }
                char top = stack.peek();
                if ((top == '(' && ch == ')') || (ch == '}' && top == '{') || (ch == ']' && top == '[')) {
                    stack.pop();
                }else {
                    return false;
                }
            }
        }
        if(stack.empty()) {
            return true;
        }
        return false;
    }

//    public static boolean isValid(String s) {
////   1.先判断字符串是否为空
//        if(s.isEmpty())
//            return true;
//        Stack<Character> stack=new Stack<Character>();
//        for(char c:s.toCharArray()){
//            if(c=='(')
//                stack.push(')');
//            else if(c=='{')
//                stack.push('}');
//            else if(c=='[')
//                stack.push(']');
//            else if(stack.empty()||c!=stack.pop())
//                return false;
//        }
//        if(stack.empty())
//            return true;
//        return false;
//    }

    //逆波兰表达式
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(String s : tokens) {
            if(!isOperation(s)) {
                //数字
                stack.push(Integer.parseInt(s));
            }else {
                // + - * / 中的一个运算符
                int num2 = stack.pop();
                int num1 = stack.pop();
                switch(s) {
                    case "+":
                        stack.push(num1 + num2);
                        break;
                    case "-":
                        stack.push(num1 - num2);
                        break;
                    case "*":
                        stack.push(num1 * num2);
                        break;
                    case "/":
                        stack.push(num1 / num2);
                        break;
                }
            }
        }
        return stack.pop();
    }
    private Boolean isOperation(String s) {
        if(s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        //链栈
        LinkedList<Integer> stack = new LinkedList<>();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        System.out.println(stack.peek());

        //顺序栈
        Stack<Integer> stack1 = new Stack<>();
    }

    public static void main3(String[] args) {
        System.out.println(isValid("{(())}"));
    }

    public static void main2(String[] args) {
        MyStack myStack = new MyStack();
        myStack.push(1);
        myStack.push(2);
        int x = myStack.pop();
        System.out.println(x);
        System.out.println(myStack.peek());
    }
    public static void main1(String[] args) {
        Stack<Integer> stack = new Stack<>();
        //放数据
        stack.push(1);
        stack.push(2);
        stack.push(3);
        //弹出数据 删除
        Integer x = stack.pop();
        System.out.println(x);
        //获取栈顶元素  不删除
        int ret = stack.peek();
        System.out.println(ret);
        //栈是否为空
        Boolean b = stack.empty();
        System.out.println(b);

        System.out.println(stack.size());
    }
}
